﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using JtextEpicsInterface;
using string_short;
using jtext_EpicsClient;
using jtext_EpicsServer;
using EpicsJecketClient;
using System.Xml;
using System.Xml.Serialization;
using EpicsJecket;
using System.IO;





namespace LibAutoTransmeter
{
    /// <summary>
    /// this describes the properties used in the autotransmeter
    /// </summary>
    [XmlInclude(typeof(TransCommand))]
    [Serializable]
    public class TranmeterProp
    {
        public string Name;
        /// <summary>
        /// when flag changes, do transmeter
        /// </summary>
        public string PvFlag;
        public ArrayList values;
        public ArrayList props;
        /// <summary>
        /// when this PV is not "null", and when =0 this transmeter will work when=1 it will not work
        /// </summary>
        public string PvDisable;

        //string strMessage;
        byte[] byteMessage;

        /// <summary>
        /// does this transmetter is the host of the flag and the values
        /// </summary>
        public bool isHost;
        public string myTemplate;
        public  bool isByte;
        public string mask;
        

        /// <summary>
        /// keep geting state instead of using monitor
        /// </summary>
        public bool noMonitor;
        /// <summary>
        /// use simple compare instead of regex
        /// </summary>
        public bool noRegex;
        /// <summary>
        /// if true it wall keep sending, till the mask does not meet.
        /// </summary>
        public bool noStop;
        public int revision;

        /// <summary>
        /// Hold the TransCommand, only availbe in AT2.0+
        /// </summary>
        public ArrayList commands;


        /// <summary>
        /// keep pv and its data type
        /// </summary>
        [XmlIgnore]
        public Hashtable Values;
        public TranmeterProp()
        {
            Values = new Hashtable();
            values = new ArrayList();
            props = new ArrayList();
            noMonitor = false;
            noRegex = false;
            PvDisable = "null";
            noMonitor = false;
            noStop = false;
            noRegex = false;
            //2.0+
            revision = 1;
            commands = null;
        }

        private void recoverFromXml()
        {
            for (int i = 0; i < values.Count; i++)
            {
                try
                {
                    Values.Add(values[i], props[i]);
                }
                catch
                {
                    //may be live time is added
                }
            }
        }

        private void doBeforeXml()
        {
            values.Clear();
            props.Clear();
            foreach (DictionaryEntry val in Values)
            {
                try
                {
                    values.Add(val.Key.ToString());
                    props.Add(val.Value.ToString());
                }
                catch { }
            }
        }

        public void saveMe(string filePath)
        {
            this.doBeforeXml();
            XmlSerializer Xs = new XmlSerializer(typeof(TranmeterProp));
            Stream Fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
            Xs.Serialize(Fs, this);
            Fs.Close();
        }

        static public TranmeterProp loadMe(string filePath)
        {
            XmlSerializer Xs = new XmlSerializer(typeof(TranmeterProp));
            Stream Fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            TranmeterProp Stm = (TranmeterProp)Xs.Deserialize(Fs);
            Fs.Close();
            Stm.recoverFromXml();
            return Stm;
        }
    
    }
    

}
